package club.xyes.pi4j.i2c;

import com.pi4j.io.i2c.I2CDevice;

import java.io.IOException;

/**
 * 2018/11/6 Create by 郭文梁
 * SSD1306Device
 * SSD1306驱动
 *
 * @author 郭文梁
 * @date 2018/11/6
 */
public class SSD1306Device implements I2CDevice {
    private PrettyIICDevice device;

    public SSD1306Device(PrettyIICDevice device) {
        this.device = device;
    }

    @Override
    public int getAddress() {
        return device.getAddress();
    }

    @Override
    public void write(byte b) throws IOException {
        device.write(b);
    }

    @Override
    public void write(byte[] buffer, int offset, int size) throws IOException {
        device.write(buffer, offset, size);
    }

    @Override
    public void write(byte[] buffer) throws IOException {
        device.write(buffer);
    }

    @Override
    public void write(int address, byte b) throws IOException {
        device.write(address, b);
    }

    @Override
    public void write(int address, byte[] buffer, int offset, int size) throws IOException {
        device.write(address, buffer, offset, size);
    }

    @Override
    public void write(int address, byte[] buffer) throws IOException {
        device.write(address, buffer);
    }

    @Override
    public int read() throws IOException {
        return device.read();
    }

    @Override
    public int read(byte[] buffer, int offset, int size) throws IOException {
        return device.read(buffer, offset, size);
    }

    @Override
    public int read(int address) throws IOException {
        return device.read(address);
    }

    @Override
    public int read(int address, byte[] buffer, int offset, int size) throws IOException {
        return device.read(address, buffer, offset, size);
    }

    @Override
    public int read(byte[] writeBuffer, int writeOffset, int writeSize, byte[] readBuffer, int readOffset, int readSize) throws IOException {
        return device.read(writeBuffer, writeOffset, writeSize, readBuffer, readOffset, readSize);
    }

    /**
     * 写命令
     *
     * @param cmd 命令
     * @throws IOException IOE
     */
    public void writeCmd(int cmd) throws IOException {
        device.write(0x78);
        device.write(0x00);
        device.write(cmd);
    }

    /**
     * 写数据
     *
     * @param data 数据
     * @throws IOException IOE
     */
    public void writeData(int data) throws IOException {
        device.write(0x78);
        device.write(0x40);
        device.write(data);
    }

    /**
     * 初始化屏幕
     *
     * @throws IOException IOE
     */
    public void init() throws IOException {
        writeCmd(0xAE);
        writeCmd(0x00);
        writeCmd(0x10);
        writeCmd(0x40);
        writeCmd(0xB0);
        writeCmd(0x81);
        writeCmd(0xFF);
        writeCmd(0xA1);
        writeCmd(0xA6);
        writeCmd(0xA8);
        writeCmd(0x3F);
        writeCmd(0xC8);
        writeCmd(0xD3);
        writeCmd(0x00);
        writeCmd(0xD5);
        writeCmd(0x80);
        writeCmd(0xD8);
        writeCmd(0x05);
        writeCmd(0xD9);
        writeCmd(0xF1);
        writeCmd(0xDA);
        writeCmd(0x12);
        writeCmd(0xDB);
        writeCmd(0x30);
        writeCmd(0x8D);
        writeCmd(0x14);
    }

    /**
     * 设置当前指针未知
     *
     * @param x 横轴 0-127
     * @param y 纵轴 0-7
     */
    public void setPos(int x, int y) throws IOException {
        writeCmd(0xb0 + y);
        writeCmd(((x & 0xf0) >> 4) | 0x10);
        writeCmd(x & 0x0f);
    }
}
